<template>
  <el-main>
    <div class="margin-bottom-20">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/audit' }"
          >日志管理
        </el-breadcrumb-item>
        <el-breadcrumb-item>登录日志</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="clearfix margin-bottom-10">
      <div class="pull-left">
        <el-button-group>
          <el-button @click="handleDayDownload(3)" size="medium" type="primary"
            >3日报表</el-button
          >
          <el-button @click="handleDayDownload(7)" size="medium" type="primary"
            >7日报表</el-button
          >
          <el-button @click="handleDayDownload(15)" size="medium" type="primary"
            >半月报表</el-button
          >
        </el-button-group>
      </div>
      <div class="pull-right">
        <el-button-group size="medium">
          <el-button @click="handleDelete" size="medium" type="primary"
            >删除日志</el-button
          >
          <el-button @click="handleDownload" size="medium" type="primary"
            >批量下载</el-button
          >
          <el-button @click="load_table_data" size="medium" type="primary"
            >刷新</el-button
          >
        </el-button-group>
      </div>
    </div>
    <el-card class="box-card" shadow="never">
      <el-table
        :data="table_data"
        @selection-change="handleSelectionChange"
        stripe
        style="width: 100%"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column
          label="编号"
          prop="id"
          show-overflow-tooltip
          width="100"
        ></el-table-column>
        <el-table-column
          label="登录账户"
          prop="accountName"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="IP地址"
          prop="ip"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="登录终端"
          prop="agent"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="操作系统"
          prop="os"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="登录状态" width="80">
          <template slot-scope="scope">
            <el-tag
              disable-transitions
              type="success"
              v-if="scope.row.logState"
            >
              成功
            </el-tag>
            <el-tag disable-transitions type="danger" v-else>
              失败
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作信息"
          prop="remark"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="登录时间"
          prop="ctime"
          show-overflow-tooltip
        ></el-table-column>
      </el-table>
      <div class="margin-top-10 clearfix">
        <el-pagination
          :current-page.sync="current_page"
          :page-size="page_size"
          :page-sizes="[5, 10, 20]"
          :total="total_size"
          @current-change="load_table_data"
          @size-change="load_table_data"
          background
          class="pull-right"
          layout="total, sizes,  prev, pager, next"
        />
      </div>
    </el-card>
  </el-main>
</template>

<script>
import { mapActions, mapState } from "vuex";

export default {
  data() {
    return {
      multipleSelection: []
    };
  },
  computed: {
    ...mapState("user_login_log", ["total_size", "table_data"]),
    page_size: {
      get() {
        return this.$store.state.user_login_log.page_size;
      },
      set(value) {
        this.set_page_size(value);
      }
    },
    current_page: {
      get() {
        return this.$store.state.user_login_log.current_page;
      },
      set(value) {
        this.set_current_page(value);
      }
    },
    search_param: {
      get() {
        return this.$store.state.user_login_log.search_param;
      }
    }
  },
  mounted() {
    this.load_table_data();
  },
  methods: {
    ...mapActions("user_login_log", [
      "load_table_data",
      "set_page_size",
      "set_current_page",
      "set_search_param"
    ]),
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleDelete() {
      const ids = this.multipleSelection.map(item => {
        return item.id;
      });

      if (ids.length == 0) {
        return;
      }

      this.$confirm("此操作将永久删除选中的日志, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let loadingInstance = this.$loading({ fullscreen: true });
        this.$ajax.audit
          .user_login_log_delete({ data: ids })
          .then(result => {
            if (result.code != 1) {
              this.$message({
                message: result.msg,
                type: "warning"
              });
            } else {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.load_table_data();
            }
          })
          .catch(e => {
            console.log(e);
            this.$message({
              message: "系统繁忙，请稍后重试",
              type: "warning"
            });
          })
          .finally(function() {
            loadingInstance.close();
          });
      });
    },
    handleDownload() {
      const ids = this.multipleSelection.map(item => {
        return item.id;
      });

      if (ids.length == 0) {
        return;
      }

      let loadingInstance = this.$loading({ fullscreen: true });
      this.$ajax.audit
        .user_login_log_download({ data: ids })
        .then(result => {
          console.log(result);
          this.download(result);
        })
        .catch(e => {
          console.log(e);
          this.$message({
            message: "系统繁忙，请稍后重试",
            type: "warning"
          });
        })
        .finally(function() {
          loadingInstance.close();
        });
    },
    download(data) {
      if (!data) {
        return;
      }
      let url = window.URL.createObjectURL(new Blob([data]));
      let link = document.createElement("a");
      link.style.display = "none";
      link.href = url;
      link.setAttribute("download", "登录日志.xls");
      document.body.appendChild(link);
      link.click();
    },
    handleDayDownload(day) {
      let loadingInstance = this.$loading({ fullscreen: true });
      this.$ajax.audit
        .user_login_log_day_download(day)
        .then(result => {
          console.log(result);
          this.download(result);
        })
        .catch(e => {
          console.log(e);
          this.$message({
            message: "系统繁忙，请稍后重试",
            type: "warning"
          });
        })
        .finally(function() {
          loadingInstance.close();
        });
    }
  }
};
</script>
